package logic

import (
	"api/pkg"
	"context"
	"strconv"
	"usersrv/user"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginUserLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewLoginUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginUserLogic {
	return &LoginUserLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *LoginUserLogic) LoginUser(req *types.LoginUserRequest) (resp *types.Response, err error) {
	// todo: add your logic here and delete this line
	loginUser, err := l.svcCtx.UserSrv.LoginUser(l.ctx, &user.LoginUserReq{
		Mobile:   req.Mobile,
		Password: req.Passweord,
		SendSms:  req.SendSms,
	})
	if err != nil {
		return &types.Response{
			Code:     500,
			Message:  err.Error(),
			Database: nil,
		}, nil
	}
	handler, err := pkg.TokenHandler(strconv.FormatInt(loginUser.Id, 10))
	if err != nil {
		return &types.Response{
			Code:     500,
			Message:  err.Error(),
			Database: nil,
		}, nil
	}
	return &types.Response{
		Code:     200,
		Message:  "用户登录成功",
		Database: handler,
	}, nil
}
